package com.zhl.config;

import com.other.CustomeFeignConfigure;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.core.type.AnnotationMetadata;

public class CustomBeanDefinitionRegistryProcessor implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        if ("FeignContext-123".equals(applicationContext.getDisplayName())) {
//            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(LocalFeignTargeter.class);
//            // 获取 BeanDefinition
//            org.springframework.beans.factory.config.BeanDefinition beanDefinition = builder.getBeanDefinition();
//            // 注册 BeanDefinition 到 BeanFactory
//            registry.registerBeanDefinition("localFeignTargeter", beanDefinition);
//
//            BeanDefinitionBuilder builder0 = BeanDefinitionBuilder.genericBeanDefinition(FeignLocal.class);
//            // 获取 BeanDefinition
//            org.springframework.beans.factory.config.BeanDefinition beanDefinition0 = builder0.getBeanDefinition();
//            // 注册 BeanDefinition 到 BeanFactory
//            registry.registerBeanDefinition("FeignLocal", beanDefinition0);
//
//
//            BeanDefinitionBuilder builder1 = BeanDefinitionBuilder.genericBeanDefinition(FeignLocal.Builder.class);
//            // 获取 BeanDefinition
//            org.springframework.beans.factory.config.BeanDefinition beanDefinition1 = builder1.getBeanDefinition();
//            beanDefinition1.setBeanClassName(FeignLocal.Builder.class.getName());
//            beanDefinition1.setFactoryBeanName("FeignLocal");
//            beanDefinition1.setFactoryMethodName("builder");
//
//            // 注册 BeanDefinition 到 BeanFactory
//            registry.registerBeanDefinition("FeignLocal.builder", beanDefinition1);


            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomeFeignConfigure.class);
            registry.registerBeanDefinition("CustomeFeignConfigure", builder.getBeanDefinition());
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if ("FeignContext-123".equals(applicationContext.getDisplayName())){
//            beanFactory.registerSingleton("LocalFeignMark", new LocalFeignMark());
//            DispatcherServlet dispatcherServlet = beanFactory.getBean(DispatcherServlet.class);
//
//            beanFactory.registerSingleton("localFeignTargeter", new LocalFeignTargeter(dispatcherServlet));
//            beanFactory.registerSingleton("FeignLocal.builder", FeignLocal.builder());

//            beanFactory.registerSingleton("CustomeFeignConfigure", new  CustomeFeignConfigure());
        }


        //beanFactory.registerSingleton("customeFeignConfigure", new CustomeFeignConfigure());
        System.out.println(beanFactory.getBeanDefinitionNames());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
